package de.java.shortys.myhome.api.view;

import static de.java.shortys.myhome.api.images.ImageConstants.ICON_CLOSE;
import static de.java.shortys.myhome.viewconfigurator.configuration.ViewConfigurationConstants.SWINGSUBMODULEVIEW_HEAD_FOREGROUND;
import static de.java.shortys.myhome.viewconfigurator.configuration.ViewConfigurationConstants.SWINGSUBMODULEVIEW_HEAD_HIGHLIGHT;
import static de.java.shortys.myhome.viewconfigurator.configuration.ViewConfigurationConstants.SWINGSUBMODULEVIEW_HEAD_SHADOW;
import static de.java.shortys.myhome.viewconfigurator.controller.ViewConfigurationManager.getInstance;
import static java.awt.BorderLayout.EAST;
import static java.awt.FlowLayout.RIGHT;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.java.shortys.myhome.viewconfigurator.controller.ViewConfigurationManager;

public class SwingSubModuleHead extends JPanel {

	private JLabel lTitle;
	private JPanel menuPane;
	private JButton menuItemClose;

	public SwingSubModuleHead() {
		initialize();
	}

	public void setDeactivatable(boolean deactivatable) {
		if (!deactivatable) {
			menuPane.remove(menuItemClose);
		}
	}

	private void createMenu() {
		menuPane = new JPanel();
		menuPane.setPreferredSize(new Dimension(25, 25));
		menuPane.setOpaque(false);
		menuPane.setLayout(new FlowLayout(RIGHT));
		add(EAST, menuPane);

		menuItemClose = new JButton(ICON_CLOSE);
		menuItemClose.setBorder(null);
		menuItemClose.setBorderPainted(false);
		menuPane.add(menuItemClose);

	}

	public void addCloseListener(ActionListener listener) {
		menuItemClose.addActionListener(listener);
	}

	private void createTitle() {
		lTitle = new JLabel();
		lTitle.setForeground(ViewConfigurationManager.getInstance().getColor(SWINGSUBMODULEVIEW_HEAD_FOREGROUND));
		Font font = lTitle.getFont();
		lTitle.setFont(new Font(font.getName(), font.getStyle(), font.getSize() + 2));
		lTitle.setOpaque(false);
		lTitle.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		add(lTitle);
	}

	public String getTitle() {
		if (lTitle != null) {
			return lTitle.getText();
		}

		return null;
	}

	private void initialize() {
		setLayout(new BorderLayout());
		Dimension headSize = new Dimension(0, 30);
		setPreferredSize(headSize);
		setMinimumSize(headSize);
		createTitle();
		createMenu();
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setPaint(new GradientPaint(0f, 0f, getInstance().getColor(SWINGSUBMODULEVIEW_HEAD_HIGHLIGHT), 0f, 85f,
				getInstance().getColor(SWINGSUBMODULEVIEW_HEAD_SHADOW)));
		g2.fillRect(0, 0, this.getSize().width, this.getSize().height);

	}

	public void setTitle(String title) {
		if (lTitle != null) {
			lTitle.setText(title);
		}
	}

}
